// import { ex16hex, crc16Modbus, decToHex } from './hex';

const { ex16hex, crc16Modbus, decToHex } = require('./hex');

module.exports = function checkSockDataFn(
  sendData = null,
  sockData = null,
  connectType = null
) {
  let newSendData = sendData;

  let newSockData = sockData;

  if (connectType === 'Modbus TCP/IP') {
    const sendMbap = newSendData.slice(0, 8);

    const sockMbap = newSockData.slice(0, 8);

    const sockLeng = newSockData.slice(8, 12);

    const sockRealData = newSockData.slice(12);

    const sockDataLength = decToHex(sockRealData.length / 2, 2);

    if (sendMbap !== sockMbap && sockLeng === sockDataLength) {
      return false;
    }

    newSendData = newSendData.slice(12);

    newSockData = newSockData.slice(12);
  }

  if (newSendData && newSockData && newSockData.length >= 6) {
    // 从机地址
    const sendAddress = newSendData.slice(0, 2);
    const sockAddress = newSockData.slice(0, 2);
    // 功能码
    const sendFunCode = newSendData.slice(2, 4);
    const sockFunCode = newSockData.slice(2, 4);

    let type = null;

    let writeType = null;

    let readType = null;

    switch (sendFunCode) {
      case '01':
      case '02':
        type = 'read';
        readType = 'bool';
        break;
      case '03':
      case '04':
        type = 'read';
        readType = 'val';
        break;
      case '05':
      case '06':
        type = 'write';
        writeType = 'single';
        break;
      case '10':
      case '0F':
        type = 'write';
        writeType = 'multiple';
        break;
      default:
        break;
    }

    if (type) {
      const crc = newSockData.slice(newSockData.length - 4);

      if (type === 'read') {
        if (
          sendAddress.toUpperCase() === sockAddress.toUpperCase() &&
          sendFunCode.toUpperCase() === sockFunCode.toUpperCase()
        ) {
          if (readType === 'val') {
            const sendNum = ex16hex(newSendData.slice(8, 12));

            const sockNum = ex16hex(newSockData.slice(4, 6));

            if (sendNum * 2 === sockNum) {
              const sockLeng = newSockData.slice(6);

              if (
                sockLeng.length ===
                sockNum * 2 + (connectType === 'Modbus TCP/IP' ? 0 : 4)
              ) {
                if (connectType === 'Modbus TCP/IP') {
                  return true;
                }

                if (
                  crc16Modbus(newSockData.slice(0, newSockData.length - 4)) ===
                  crc
                ) {
                  return true;
                }
              }
            }
          }

          if (readType === 'bool') {
            const sockNum = ex16hex(newSockData.slice(4, 6));

            const boolVal = newSockData.slice(
              6,
              newSockData.length - (connectType === 'Modbus TCP/IP' ? 0 : 4)
            );

            if (boolVal.length === sockNum * 2) {
              if (connectType === 'Modbus TCP/IP') {
                return true;
              }

              if (
                crc16Modbus(newSockData.slice(0, newSockData.length - 4)) ===
                crc
              ) {
                return true;
              }
            }
          }
        }
      }

      if (type === 'write') {
        if (
          writeType === 'single' &&
          newSendData.toUpperCase() === newSockData.toUpperCase()
        ) {
          return true;
        }

        if (
          writeType === 'multiple' &&
          newSockData.length === 16 - (connectType === 'Modbus TCP/IP' ? 4 : 0)
        ) {
          const sendLeng = newSendData.slice(0, 12);

          const writeLeng = newSockData.slice(0, 12);

          if (sendLeng === writeLeng) {
            if (connectType === 'Modbus TCP/IP') {
              return true;
            }

            if (
              crc16Modbus(newSockData.slice(0, newSockData.length - 4)) === crc
            ) {
              return true;
            }
          }
        }
      }
    }
  }

  return false;
}
